package com.jjty.dataGenerator.jserji.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjty.dataGenerator.jserji.entity.JsDataAll;
import com.jjty.dataGenerator.jserji.mapper.JsDataAllMapper;
import com.jjty.dataGenerator.jserji.utils.StudentDataFiller;
import com.jjty.dataGenerator.jserji.utils.StudentNumberGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentNumberService extends ServiceImpl<JsDataAllMapper, JsDataAll> {

    @Transactional(rollbackFor = Exception.class)
    public void fillStudentNumber() {
        // 1. 查询所有需要处理的学校数据
        LambdaQueryWrapper<JsDataAll> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(JsDataAll::getSchoolName,
                "泰州市爱的摇篮幼儿园",
                "泰兴市长生幼儿园",
                "靖江市地中海幼儿园",
                "泰州医药高新区乐童幼儿园",
                "泰州市姜堰区运粮幼儿园",
                "兴化市戴南镇阳光幼儿园"
        );
        List<JsDataAll> studentList = list(queryWrapper);

        // 2. 按学校和年份分组，每个组合单独计数（序号从 001 开始）
        // 外层 Map：学校名称 → 内层 Map
        // 内层 Map：年份 → 计数器
        Map<String, Map<String, Integer>> schoolYearCounters = new HashMap<>();

        for (JsDataAll student : studentList) {
            String schoolName = student.getSchoolName();

            // 获取当前学校的年份计数器 Map，不存在则创建
            Map<String, Integer> yearCounter = schoolYearCounters.computeIfAbsent(
                    schoolName, k -> new HashMap<>()
            );

            // 生成随机年份（2024/2023/2022）
            String year = StudentNumberGenerator.generateRandomYear();

            // 获取当前年份的计数器，不存在则初始化为 1
            int sequence = yearCounter.computeIfAbsent(year, k -> 1);

            // 生成学号
            String studentNumber = StudentNumberGenerator.generate(schoolName, year, sequence);
            student.setStudentNumber(studentNumber);

            // 计数器递增
            yearCounter.put(year, sequence + 1);
        }

        // 3. 批量更新数据库
        if (!studentList.isEmpty()) {
            updateBatchById(studentList);
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void fillStudentFields() {
        // 1. 查询所有 student_number 不为空的记录
        LambdaQueryWrapper<JsDataAll> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(JsDataAll::getStudentNumber);
        List<JsDataAll> studentList = list(queryWrapper);

        // 2. 批量填充字段（只处理空值）
        for (JsDataAll student : studentList) {
            String studentNumber = student.getStudentNumber();

            // 填充年级（仅当 grade 为空时）
            if (student.getGrade() == null) {
                student.setGrade(StudentDataFiller.fillGrade(studentNumber));
            }

            // 填充班级（仅当 classNumber 为空时）
            if (student.getClassNumber() == null) {
                student.setClassNumber(StudentDataFiller.fillClassNumber());
            }

            // 填充亲属关系（仅当 relationship 为空时）
            if (student.getRelationship() == null) {
                student.setRelationship(StudentDataFiller.fillRelationship());
            }

            // 填充性别（仅当 sex 为空时）
            if (student.getSex() == null) {
                student.setSex(StudentDataFiller.fillSex());
            }
        }

        // 3. 批量更新数据库
        if (!studentList.isEmpty()) {
            updateBatchById(studentList);
        }
    }
}